<template>
<el-config-provider :locale="locale">
   <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>项目管理</el-breadcrumb-item>
      <el-breadcrumb-item>项目信息管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
     <el-card >
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
           <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getProjectList" >
              <template #append>
                <el-button  :icon="Search"  @click="getProjectList"/>
              </template>
            </el-input>
        </el-col>
        <el-col :span="4">
           <el-button type="primary" @click="addDialogVisible=true">添加项目</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="projectList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column prop="ProjectName" label="项目名称"  />
        <el-table-column prop="ProjectPrice" label="项目单价"  />
        <el-table-column prop="ServicePrice" label="上门服务费"  />
        <el-table-column prop="NursePrice" label="护理费"  />
        <el-table-column prop="ProjectDescribe" label="项目描述"  />
        <el-table-column prop="CateID" label="项目分类编号"  />
        <el-table-column  label="操作" >
          <!-- scope是一个插槽 -->
          <template #default="scope">  
            <!-- 编辑按钮 -->
            <el-button size="small" @click="showEditDialog( scope.row.id)">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button size="small" type="danger" @click="deleteById( scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination  v-model:currentPage="queryInfo.pagenum" v-model:page-size="queryInfo.pagesize"
      :page-sizes="[2, 3, 5, 10]" :small="small" :disabled="disabled" :background="background"
      layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange"  @current-change="handleCurrentChange" />

    </el-card>
    <div>提示：项目分类编号对应数字为 "1":医疗护理; "2":就诊陪同; "3":专业护理; "4":康复中医</div>

    <!-- 添加用户对话框 -->
    <el-dialog v-model="addDialogVisible" title="添加项目" width="50%" @close="addDialogClosed">
      <!-- 表单内容区域 -->
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="80px"  :size="formSize">
        <el-form-item label="项目名称" prop="ProjectName">
          <el-input v-model="addForm.ProjectName" />
        </el-form-item>
        <el-form-item label="项目单价" prop="ProjectPrice">
          <el-input v-model="addForm.ProjectPrice"/>
        </el-form-item>
        <el-form-item label="上门服务费" prop="ServicePrice">
          <el-input v-model="addForm.ServicePrice"/>
        </el-form-item>
        <el-form-item label="护理费" prop="NursePrice">
          <el-input v-model="addForm.NursePrice"/>
        </el-form-item>
        <el-form-item label="项目描述" prop="ProjectDescribe">
          <el-input v-model="addForm.ProjectDescribe"/>
        </el-form-item>
        <el-form-item label="分类编号" prop="CateID">
          <el-select  v-model="addForm.CateID" clearable placeholder="请选择项目分类编号" style="width:100%">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addProject">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 修改用户对话框 -->
    <el-dialog v-model="editDialogVisible" title="编辑项目" width="50%" @close="editDialogClosed">
      <!-- 表单内容区域 -->
      <el-form ref="editFormRef" :model="editForm" :rules="editFormRules" label-width="80px"  :size="formSize">
        <el-form-item label="项目名称" prop="ProjectName">
          <el-input v-model="editForm.ProjectName" />
        </el-form-item>
        <el-form-item label="项目单价" prop="ProjectPrice">
          <el-input v-model="editForm.ProjectPrice"/>
        </el-form-item>
        <el-form-item label="上门服务费" prop="ServicePrice">
          <el-input v-model="editForm.ServicePrice"/>
        </el-form-item>
        <el-form-item label="护理费" prop="NursePrice">
          <el-input v-model="editForm.NursePrice"/>
        </el-form-item>
        <el-form-item label="项目描述" prop="ProjectDescribe">
          <el-input v-model="editForm.ProjectDescribe"/>
        </el-form-item>
        <el-form-item label="分类编号" prop="CateID">
          <el-select  v-model="editForm.CateID" clearable placeholder="请选择项目分类编号" style="width:100%">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editProjectInfo">确定</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
  </el-config-provider>
  
</template>

<script>
import { ElConfigProvider } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { ArrowRight } from '@element-plus/icons-vue'
import { Search } from '@element-plus/icons-vue'
import qs from 'qs'
import {  ElMessageBox } from 'element-plus'
import { ref } from 'vue'
export default {
    components:{
    ArrowRight,
    ElConfigProvider,
    ElMessageBox

 },
  setup(){
    return{
      Search,
      locale: zhCn
    }
  },

  data(){
    return{
      //获取项目列表的参数对象
      queryInfo:{
        query:'',
        //当前页数
        pagenum:1,
        //当前每页显示多少调数据
        pagesize:2,
        
      },
      projectList:[],
      total:0,
      //控制添加项目对话框的显示与隐藏
      addDialogVisible:false,
      //添加项目的表单数据
      addForm:{
        ProjectName:'',
        ProjectPrice:'',
        ServicePrice:'',
        NursePrice:'',
        ProjectDescribe:'',
        CateID:'',
      },
      //添加表单的验证规则对象
      addFormRules:{
        ProjectName:[{
          required:true,message:'请输入项目名称',trigger:'blur'
        }],
        ProjectPrice:[{
          required:true,message:'请输入项目单价',trigger:'blur'
        }],
        ServicePrice:[{
          required:true,message:'请输入上门服务费',trigger:'blur'
        }],
        NursePrice:[{
          required:true,message:'请输入护理费',trigger:'blur'
        }],
        ProjectDescribe:[{
          required:true,message:'请描述项目内容',trigger:'blur'
        }],
        CateID:[{
          required:true,message:'请选择分类编号',trigger:'blur'
        }],
      },
      //控制编辑项目对话框的显示与隐藏
      editDialogVisible:false,
      //查询到项目的信息对象
      editForm:{},
      //修改表单的验证规则对象
      editFormRules:{
        ProjectName:[{
          required:true,message:'请输入项目名称',trigger:'blur'
        }],
        ProjectPrice:[{
          required:true,message:'请输入项目单价',trigger:'blur'
        }],
        ServicePrice:[{
          required:true,message:'请输入上门服务费',trigger:'blur'
        }],
        NursePrice:[{
          required:true,message:'请输入护理费',trigger:'blur'
        }],
        ProjectDescribe:[{
          required:true,message:'请描述项目内容',trigger:'blur'
        }],
        CateID:[{
          required:true,message:'请选择分类编号',trigger:'blur'
        }],

      },
    }

  },

  created(){
    this.getProjectList()
  },

  methods:{
    async getProjectList(){
      const {data:res} =await this.$http.get('/my/project/getproject',{ params:this.queryInfo})
      if(res.meta.status!==200) {
        return this.$message.err('获取项目信息列表失败')
      }
      this.projectList=res.data.project
      this.total=res.data.total
      console.log(res)
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize){
      //console.log(newSize)
      this.queryInfo.pagesize=newSize
      this.getProjectList()

    },
    //监听页码值改变的事件
    handleCurrentChange(newPage){
      console.log(newPage)
      this.queryInfo.pagenum=newPage
      this.getProjectList()
    },
    //监听对话框的关闭事件resetFields()
    addDialogClosed(){
      this.$refs.addFormRef.resetFields()
    },

    //添加新项目
    addProject(){
      this.$refs.addFormRef.validate(async valid=>{
        if(!valid)  return 
        //发出添加项目的网络请求
        const {data:res} =await this.$http.post('/my/project/addproject',qs.stringify(this.addForm))
        console.log(res)
        if(res.meta.status!==201){
          this.$message.Error('添加项目失败')
        }
        this.$message.success('添加项目成功')
        //隐藏项目对话框
        this.addDialogVisible=false
        //重新获取项目列表数据
        this.getProjectList()
      })
    },

    //展示编辑的对话框
    async showEditDialog(id){
      //console.log(id)
      const {data:res} =await this.$http.get('/my/project/getprojectbyid/'+id)
      console.log(res)
      if(res.meta.status!==200) { 
        return this.$message.error('查询项目信息失败')
      }
      this.editForm=res.data
      this.editDialogVisible=true
    },

    //监听修改项目对话框的关闭事件
    editDialogClosed(){
      this.$refs.editFormRef.resetFields()
    },

    //修改项目信息并提交
    editProjectInfo(){ 
      this.$refs.editFormRef.validate( async valid=>{
        if(!valid) return 
        //发起修改项目信息的数据请求
        //this.editForm=JSON.parse(JSON.stringify(this.editForm))
        var data = qs.stringify(this.editForm)
        const {data:res} = await this.$http.post('/my/project/updateproject/'+ this.editForm.id, data,
        {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
        //判断请求是否成功
        if (res.meta.status !== 200) {
          return this.$message.error('更新项目信息失败！')
        }
        //关闭对话框
        this.editDialogVisible=false
        //刷新数据列表
        this.getProjectList()
        //提示修改成功
        this.$message.success('更新项目信息成功！')
      })
    },

    //根据id删除项目信息
    async deleteById(id){
      //console.log(id)
      //弹框提示是否删除数据
      const confirmResult=await ElMessageBox.confirm('此操作将永久删除该项目，是否继续?','提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch(err=>err)
      //如果项目确认删除，返回字符串confirm
      //console.log(confirmResult)
      //如果项目取消了删除，返回值为字符串cancel
      if(confirmResult!=='confirm'){
      return  this.$message.info('已取消删除')
      }
      const {data:res} =await this.$http.get('/my/project/deleteproject/'+id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除信息失败！')
      }
      this.$message.success('删除信息成功！')
    
    }

  },

}
</script>

<style lang="less" scoped>

</style>